package com.cat.dataStructure;

import java.util.HashMap;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/count-special-triplets/description/
 * @create 2025/8/15 10:12
 * @since JDK17
 */

public class Solution14 {
    static int N = ((int) (1e9 + 7));
    static HashMap<Integer, Integer> pre = new HashMap<>();
    static HashMap<Integer, Integer> suf = new HashMap<>();
    public int specialTriplets(int[] nums) {
        int n = nums.length - 1, ans = 0, cnt;
        pre.clear();
        suf.clear();
        // 记录后缀
        for (int i = n; i > 0; i--) {
            cnt = suf.getOrDefault(nums[i], 0) + 1;
            suf.put(nums[i], cnt);
        }
        pre.put(nums[0], 1);
        for (int i = 1, t1, t2; i < n; i++) {   // 枚举中间位置
            suf.put(nums[i], suf.get(nums[i]) - 1); // 踢出去
            t1 = pre.getOrDefault(nums[i] * 2, 0);
            t2 = suf.getOrDefault(nums[i] * 2, 0);
            ans = (ans + t1 * t2) % N;
            pre.put(nums[i], pre.getOrDefault(nums[i], 0) + 1);
        }
        return ans;
    }
}
